package com.wjb.xietong.W3;

import java.util.Map;

import com.wjb.xietong.Volley.AuthFailureError;
import com.wjb.xietong.Volley.NetworkResponse;
import com.wjb.xietong.Volley.Request;
import com.wjb.xietong.Volley.Request.Method;
import com.wjb.xietong.Volley.Response;
import com.wjb.xietong.Volley.Response.ErrorListener;
import com.wjb.xietong.Volley.Response.Listener;


public abstract class Post<T> extends Request<T> {
    /** Charset for request. */
    private static final String PROTOCOL_CHARSET = "utf-8";

    /** Content type for request. */
    private static final String PROTOCOL_CONTENT_TYPE =
        String.format("application/x-www-form-urlencoded; charset=utf-8;", PROTOCOL_CHARSET);

    private final Listener<T> mListener;
    private final Map<String, String> mParams;

    /**
     * Deprecated constructor for a PostRequest which defaults to GET unless {@link #getPostBody()}
     * or {@link #getPostParams()} is overridden (which defaults to POST).
     *
     * @deprecated Use {@link #PostRequest(int, String, String, Listener, ErrorListener)}.
     */
    public Post(String url, Map<String, String> params, Listener<T> listener,
            ErrorListener errorListener) {
        this(Method.DEPRECATED_GET_OR_POST, url, params, listener, errorListener);
    }

    public Post(int method, String url, Map<String, String> params, Listener<T> listener,
            ErrorListener errorListener) {
        super(method, url, errorListener);
        mListener = listener;
        mParams = params;
    }

    @Override
    protected void deliverResponse(T response) {
        mListener.onResponse(response);
    }

    @Override
    abstract protected Response<T> parseNetworkResponse(NetworkResponse response);

    /**
     * @deprecated Use {@link #getBodyContentType()}.
     */
    @Override
    public String getPostBodyContentType() {
        return getBodyContentType();
    }

    @Override
    public String getBodyContentType() {
        return PROTOCOL_CONTENT_TYPE;
    }
    
    @Override
	protected Map<String, String> getParams() throws AuthFailureError {
		// TODO Auto-generated method stub
		return mParams;
	}
}
